package com.agent.history.comsumer;

import com.agent.commons.other.MessageHistory;
import com.agent.commons.utils.JacksonUtil;
import com.agent.history.service.ConversationService;
import com.agent.history.service.MessageHistoryService;
import com.agent.history.vo.ConversationListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import static com.agent.commons.constants.RedisConstants.*;
import static com.agent.commons.constants.RabbitMqConstants.*;

import java.util.concurrent.TimeUnit;

/**
 * mq消费者
 */
@Component
@Slf4j
public class ConversationComsumer {

    @Resource
    private MessageHistoryService messageHistoryService;
    @Resource
    private ConversationService  conversationService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 把聊天记录存入数据库
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value =QUEUE_CONVERSATION_HISTORY,durable ="true"),
            exchange=@Exchange(value=DIRECT_CONVERSATION,type= ExchangeTypes.DIRECT),
            key = {"message"},
            arguments = @Argument(name = "x-delay",value = "lazy")
    ))
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value =QUEUE_CONVERSATION_HISTORY,durable ="true"),
            exchange=@Exchange(value=FANOUT_CONVERSATION,type= ExchangeTypes.FANOUT),
            arguments = @Argument(name = "x-delay",value = "lazy")
    ))
    public void writeMessageHistory(MessageHistory message){
        log.info("conversation.history.queue:{}",message);
        messageHistoryService.addMessageHistory(message);
    }

    /**
     * 监听conversation.id.queue，判断是否为新建会话，存储会话Id
     * @param messageHistory
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_CONVERSATION_ID,durable ="true"),
            exchange=@Exchange(value=FANOUT_CONVERSATION,type= ExchangeTypes.FANOUT),
            arguments = @Argument(name = "x-delay",value = "lazy")
    ))
    public void writeConversationId(MessageHistory messageHistory){
        //监听到conversation.id.queue消息
        log.info("conversation.id.queue:{}",messageHistory);
        //添加会话id到数据库中
        conversationService.addConversation(messageHistory);
        //根据新建会话的userId，判断Redis中是否有存储userId对应的会话Id列表
        if(!conversationService.getConversationListVOFromRedis(messageHistory.getUserId()).isEmpty()){
            //获取新建会话信息
            ConversationListVO conversationListVO=new ConversationListVO();
            conversationListVO.setConversationId(messageHistory.getConversationId());
            conversationListVO.setConversationName(messageHistory.getConversationName());
            //存在，将该会话信息添加到Redis中
            stringRedisTemplate.opsForZSet().add(MESSAGE_HISTORY+messageHistory.getUserId(),JacksonUtil.writeValueAsString(conversationListVO),System.nanoTime());
            stringRedisTemplate.expire(MESSAGE_HISTORY+messageHistory.getUserId(),7, TimeUnit.DAYS);
        }
    }


}
